﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Movistar.SMS.OutlookAddIn
{
    public partial class HistoricForm : Form
    {
        private RemindersDB remindersDB = null;

        #region Form Open Close Events

        public HistoricForm()
        {
            InitializeComponent();

            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(OutlookAddIn.Properties.Settings.Default.RegKeyAddIn);

                remindersDB = new RemindersDB(key.GetValue(OutlookAddIn.Properties.Settings.Default.RegKeyDB).ToString());

                LoadHistoric();
            }
            catch
            {
                DialogResult = DialogResult.Abort; // Close and propagate error to caller 
            }
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        #endregion

        private void LoadHistoric()
        {
            dataGrid.Rows.Clear();

            try
            {
                var allHistorics = from h in remindersDB.Historic
                                   join ap in remindersDB.Appointments
                                   on h.AppointmentID_ equals ap.AppointmentID_
                                   orderby h.Date descending
                                   select new
                                   {
                                       Subject = ap.Subject,
                                       DateAndTime = h.Date,
                                       Message = h.Message,
                                       To = h.SentTo,
                                       Status = (h.SentOK ? "OK" : "Fallo")
                                   };

                foreach (var h in allHistorics)
                    dataGrid.Rows.Add(h.Subject, h.DateAndTime, h.Message, h.To, h.Status);

                dataGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

                var pastHis = from h in remindersDB.Historic
                              join ap in remindersDB.Appointments
                              on h.AppointmentID_ equals ap.AppointmentID_
                              where ap.Occurs < DateTime.Now
                              select h;

                btBorrar.Enabled = (pastHis.Count() > 0);
            }
            catch
            {
                DialogResult = DialogResult.Abort; // Close and propagate error to caller 
            }
        }

        private void btBorrar_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("¿Está seguro de que desea eliminar los datos de envíos pasados?", "Movistar SMS",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.Cancel)
                    return;

                var pastHis = from h in remindersDB.Historic
                              join ap in remindersDB.Appointments
                              on h.AppointmentID_ equals ap.AppointmentID_
                              where ap.Occurs < DateTime.Now
                              select h;

                remindersDB.Historic.DeleteAllOnSubmit(pastHis);

                remindersDB.SubmitChanges();

                LoadHistoric();
            }
            catch
            {
                DialogResult = DialogResult.Abort; // Close and propagate error to caller 
            }
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            LoadHistoric();
        }
    }
}
